//
//  DrawView.m
//  FavDraw
//
//  Created by lang honglin on 11-2-24.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "DrawView.h"

@implementation DrawView

@synthesize firstTouch;
@synthesize lastTouch;
@synthesize currentColor;
@synthesize shapeType;
@synthesize drawImage;
@dynamic currentRect;
@dynamic redrawRect;

- (id)initWithFrame:(CGRect)frame {
    
   NSLog(@"initWithFrame");
	self = [super initWithFrame:frame];
    if (self) {
        // Initialization code.
		
  	   figureDoc = [[CFigureDoc alloc] initDoc];

    }
    return self;
}

- (CGRect)currentRect {
	return CGRectMake (
					   (firstTouch.x > lastTouch.x) ? lastTouch.x : firstTouch.x,
					   (firstTouch.y > lastTouch.y) ? lastTouch.y : firstTouch.y,
					   fabsf(firstTouch.x - lastTouch.x),
					   fabsf(firstTouch.y - lastTouch.y));
}

- (void)drawRect:(CGRect)rect {
	
	
	CGContextRef context = UIGraphicsGetCurrentContext();
	
	CGContextSetLineWidth(context, 2.0);
	CGContextSetStrokeColorWithColor(context, currentColor.CGColor);
	
	CGContextSetFillColorWithColor(context, currentColor.CGColor);
	
	switch (shapeType) {
		case kLineShape:
			CGContextMoveToPoint(context, firstTouch.x, firstTouch.y);
			CGContextAddLineToPoint(context, lastTouch.x, lastTouch.y);
			CGContextStrokePath(context);
			break;
		case kRectShape:
			CGContextAddRect(context, self.currentRect);
			CGContextDrawPath(context, kCGPathFillStroke);
			break;
		case kEllipseShape:
			CGContextAddEllipseInRect(context, self.currentRect);
			CGContextDrawPath(context, kCGPathFillStroke);
			break;
		case kImageShape: {
			CGFloat horizontalOffset = drawImage.size.width / 2;
			CGFloat verticalOffset = drawImage.size.height / 2;
			CGPoint drawPoint = CGPointMake(lastTouch.x - horizontalOffset,
											lastTouch.y - verticalOffset);
			[drawImage drawAtPoint:drawPoint];
			break;			
		}
		default:
			break;
	}
	
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
	UITouch *touch = [touches anyObject];
	firstTouch = [touch locationInView:self];
	lastTouch = [touch locationInView:self];
	
	if (shapeType == kImageShape) {
		CGFloat horizontalOffset = drawImage.size.width / 2;
		CGFloat verticalOffset = drawImage.size.height / 2;
		redrawRect = CGRectMake(firstTouch.x - horizontalOffset, firstTouch.y - verticalOffset, drawImage.size.width, drawImage.size.height);
	}
	else
		redrawRect = CGRectMake(firstTouch.x, firstTouch.y, 0, 0);
	[self setNeedsDisplay];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
	figureDoc = [[CFigureDoc alloc] initDoc];

	UITouch *touch = [touches anyObject];
	lastTouch = [touch locationInView:self];
	
	if (shapeType == kImageShape) {
		CGFloat horizontalOffset = drawImage.size.width / 2;
		CGFloat verticalOffset = drawImage.size.height / 2;
		redrawRect = CGRectUnion(redrawRect, CGRectMake(lastTouch.x - horizontalOffset, lastTouch.y - verticalOffset, drawImage.size.width, drawImage.size.height));
	}
	else if (shapeType == kLineShape) {
		NSLog(@"HereLine");
		
		[figureDoc AddFigureObj:self.currentRect];	
	}
	else
		redrawRect = CGRectUnion(redrawRect, self.currentRect);
	
	[self setNeedsDisplayInRect:redrawRect];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
	UITouch *touch = [touches anyObject];
	lastTouch = [touch locationInView:self];
	
	if (shapeType == kImageShape) {
		CGFloat horizontalOffset = drawImage.size.width / 2;
		CGFloat verticalOffset = drawImage.size.height / 2;
		redrawRect = CGRectUnion(redrawRect, CGRectMake(lastTouch.x - horizontalOffset, lastTouch.y - verticalOffset, drawImage.size.width, drawImage.size.height));
	}
	redrawRect = CGRectUnion(redrawRect, self.currentRect);
	
	[self setNeedsDisplayInRect:redrawRect];
	
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code.
}
*/

- (void)dealloc {
    [super dealloc];
	[figureDoc release];

}


@end
